package com.css.bjdt_core.reportPrepare.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.nutz.dao.Cnd;
import org.nutz.dao.Condition;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.css.bjdt_core.reportPrepare.entity.RptBatchDzzRel;
import com.css.bjdt_core.reportPrepare.entity.RptBatchUnitRel;
import com.css.bjdt_core.reportPrepare.entity.RptUnit;
import com.css.bjdt_core.reportPrepare.entity.RptUnitAnnualRel;
import com.css.bjdt_core.util.CommonUtil;
import com.css.bjdt_core.util.PageQuerys;

@Service
public class RptUnitService {
	@Autowired
	private Dao dao;

	public Map queryPageByUnitIds(int pageNum, int pageSize, List<String> unitIds) {
		Condition cnd = Cnd.where("ID", "IN", unitIds);
		return PageQuerys.ligerGridQuery(dao, RptUnit.class, cnd, pageNum, pageSize);
	}

	//@Transactional(readOnly = false)
	public void batchInsert(String dzzId, List<RptUnit> units, String annualId) {
		RptUnit rootUnit = queryByDzzIdAndAnnualId(dzzId, annualId);
		List<RptUnit> insertUnit = new ArrayList<RptUnit>();
		List<RptUnit> updateUnit = new ArrayList<RptUnit>();
		List<RptUnitAnnualRel> insertRel = new ArrayList<RptUnitAnnualRel>();
		for (RptUnit unit : units) {
			RptUnit temp = queryByDzzIdAndAnnualId(unit.getRel().getDzzId(), annualId);
			if (temp == null) {
				unit.setId(CommonUtil.getUUID());
				unit.setPid(rootUnit.getId());
				unit.setDelStatus("0");
				insertUnit.add(unit);
				RptUnitAnnualRel unitAnnualRel = new RptUnitAnnualRel();
				unitAnnualRel.setAnnualId(annualId);
				unitAnnualRel.setDzzId(unit.getRel().getDzzId());
				unitAnnualRel.setUnitId(unit.getId());
				insertRel.add(unitAnnualRel);
			} else {
				unit.setId(temp.getId());
				if (!temp.getId().equals(rootUnit.getId())) {
					temp.setPid(rootUnit.getId());
					unit.setPid(rootUnit.getId());
				}
				temp.setName(unit.getName());
				temp.setFullName(unit.getFullName());
				updateUnit.add(temp);
			}
		}
		dao.insert(insertUnit);
		dao.insert(insertRel);
		dao.updateIgnoreNull(updateUnit);
	}

	public void insertUnit(RptUnit unit) {
		dao.insert(unit);
		insertUnitAnnualRel(unit.getRel());
	}

	public void insertUnitAnnualRel(RptUnitAnnualRel unitAnnualRel) {
		dao.insert(unitAnnualRel);
	}

	public RptUnit queryByDzzIdAndAnnualId(String dzzId, String annualId) {
		RptUnitAnnualRel unitAnnualRel = dao.fetch(RptUnitAnnualRel.class,
				Cnd.where("DZZ_ID", "=", dzzId).and("ANNUAL_ID", "=", annualId));
		if (unitAnnualRel == null) {
			return null;
		}
		RptUnit unit = dao.fetch(RptUnit.class, Cnd.where("ID", "=", unitAnnualRel.getUnitId()));
		unit.setRel(unitAnnualRel);
		return unit;
	}

	public List<RptUnit> queryByPid(String pid) {
		List<RptUnit> list = dao.query(RptUnit.class, Cnd.where("PID", "=", pid).asc("order_id"));
		for (RptUnit unit : list) {
			RptUnitAnnualRel rel = dao.fetch(RptUnitAnnualRel.class, Cnd.where("UNIT_ID", "=", unit.getId()));
			unit.setRel(rel);
		}
		return list;
	}

	public List<RptUnit> queryByPid(String pid, String name) {
		List<RptUnit> list = dao.query(RptUnit.class,
				Cnd.where("PID", "=", pid).and("SIMPLENAME", "Like", "%" + name + "%").asc("order_id"));
		for (RptUnit unit : list) {
			RptUnitAnnualRel rel = dao.fetch(RptUnitAnnualRel.class, Cnd.where("UNIT_ID", "=", unit.getId()));
			unit.setRel(rel);
		}
		return list;
	}

	public RptUnit queryById(String id) {
		return dao.fetch(RptUnit.class, id);
	}

	public void updateUnit(RptUnit unit) {
		dao.updateIgnoreNull(unit);
	}

	//根据党组织ID获得当前的批次ID
	public RptBatchDzzRel queryByDzzIdGetBatchId(String dzzId) {
		return dao.fetch(RptBatchDzzRel.class, Cnd.where("DDZ_ID", "=", dzzId));
	}

	public RptBatchDzzRel queryByDzzIdAnnualId(String dzzId, String annualId) {
		return dao.fetch(RptBatchDzzRel.class, Cnd.where("DDZ_ID", "=", dzzId).and("ANNUAL_ID", "=", annualId));
	}

	//根据单位ID获得当前批次
	public RptBatchUnitRel queryByUnitIdGetBatchId(String unitId) {
		return dao.fetch(RptBatchUnitRel.class, Cnd.where("UNIT_ID", "=", unitId));
	}

	public String getdzzIdByUnitId(String unitId) {
		RptUnitAnnualRel rel = dao.fetch(RptUnitAnnualRel.class, Cnd.where("UNIT_ID", "=", unitId));
		return rel.getDzzId();
	}//根据年度ID和单位ID获得当前的党组织ID

	public RptUnitAnnualRel getArptUnitAnnualRel(String unitId, String annualId) {
		return dao.fetch(RptUnitAnnualRel.class, Cnd.where("UNIT_ID", "=", unitId).and("ANNUAL_ID", "=", annualId));
	}

	public RptUnitAnnualRel getARptUnitAnnualRelByDzzIdAnnualId(String dzzId, String annualId) {
		return dao.fetch(RptUnitAnnualRel.class, Cnd.where("DZZ_ID", "=", dzzId).and("ANNUAL_ID", "=", annualId));
	}

	public RptUnit getARptUnitByDzzIdAnnualId(String dzzId, String annualId) {
		RptUnitAnnualRel rptUnitAnnualRel = dao.fetch(RptUnitAnnualRel.class,
				Cnd.where("DZZ_ID", "=", dzzId).and("ANNUAL_ID", "=", annualId));
		return queryById(rptUnitAnnualRel.getUnitId());
	}

	public void updateUnitOrder(String unitId, int orderId) {
		Sql strSql = Sqls.create("update rpt_unit set order_id = @orderId where id = @unitId");
		strSql.params().set("orderId", orderId).set("unitId", unitId);
		dao.execute(strSql);
	}

	public List<RptBatchDzzRel> queryDDzByBatchId(String batchId) {
		return dao.query(RptBatchDzzRel.class, Cnd.where("BATCH_ID", "=", batchId));
	}

	//根据单位ID和年度ID获得当前这个单位的批次，注意：有可能返回Null 因为有可能上级单位下发，但是下级单位还没有创建单位填报信息
	public String getBatchIdByUnitIdAndAnnualId(String unitId, String annualId) {
		RptUnitAnnualRel rptUnitAnnualRel = dao.fetch(RptUnitAnnualRel.class,
				Cnd.where("UNIT_ID", "=", unitId).and("ANNUAL_ID", "=", annualId));
		if (rptUnitAnnualRel == null) {
			return null;
		} else {
			RptBatchDzzRel rptBatchDzzRel = dao.fetch(RptBatchDzzRel.class,
					Cnd.where("DDZ_ID", "=", rptUnitAnnualRel.getDzzId()).and("ANNUAL_ID", "=", annualId));
			if (rptBatchDzzRel == null) {
				return null;
			} else {
				return rptBatchDzzRel.getId();
			}
		}
	}

	public void updateUnitPid(String unitId, String pid) {
		Sql sql = Sqls.create("update RPT_UNIT set pid = @pid where id = @id");
		sql.params().set("pid", pid).set("id", unitId);
		dao.execute(sql);
	}

	public List<RptUnit> getAllInitiatedDescendantsByDzzId(String dzzId, String annualId) {
		String sqlStr = "select * from (SELECT pu.ID PID,un.ID,un.simplename,un.fullname,un.relman,un.unittype,z.DZZID,z.SJDZZID,z.ORDERNUM from RPT_UNIT un "
				+ "LEFT JOIN RPT_UNIT_ANNUAL_R r ON UN.ID=r.UNIT_ID " + "LEFT JOIN T_M_ZZINFO z ON z.DZZID=r.DZZ_ID "
				+ "LEFT JOIN T_M_ZZINFO pz ON z.SJDZZID=pz.DZZID "
				+ "LEFT JOIN RPT_UNIT_ANNUAL_R pr ON pr.DZZ_ID=pz.DZZID AND pr.ANNUAL_ID=r.ANNUAL_ID "
				+ "LEFT JOIN RPT_UNIT pu ON pr.UNIT_ID=pu.ID " + "WHERE r.ANNUAL_ID=@annualId ) t "
				+ "START WITH t.DZZID=@dzzId " + "CONNECT BY PRIOR t.DZZID=t.SJDZZID " + "ORDER BY t.ORDERNUM";
		Sql sql = Sqls.queryEntity(sqlStr);
		sql.params().set("dzzId", dzzId).set("annualId", annualId);
		sql.setEntity(dao.getEntity(RptUnit.class));
		dao.execute(sql);
		return sql.getList(RptUnit.class);
	}
}
